<?php

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;


class UserGoodsIncome extends Base
{
    public $table = 'user_goods_income';
    protected $tableLog = 'user_goods_income_log';

    protected $tableMallGoodsConfig = 'mall_goods_upgrade_config';

    protected $tablePrefix = 'zb_';


    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    /**
     * 插入用户可升级商品经验
     * @param array $data
     * @return bool
     */
    function addUserGoodsIncome(array $data)
    {
        return DB::table($this->table)->insert($data);
    }
    /**
     * 增加或减少给定列（可升级商品经验）的值
     * @param $UID
     * @param int $change_coolval
     * @param int $cool_level
     * @return int
     */
    public function incUserGoodsIncome($UID,$GID, $change_income=1)
    {
        $obj = DB::table($this->table)
            ->where('user_id', $UID)
            ->where('goods_id', $GID);
        $res = $obj->increment('income', $change_income, ['add_time'=>time()]);

        return $res;
    }

    /**
     * 用户是否已记录商品经验值
     * @param $UID
     * @param $GID
     * @return bool
     */
    public function ifUserGoodsIncome($UID, $GID):bool
    {
        return DB::table($this->table)
            ->where('user_id', $UID)
            ->where('goods_id', $GID)
            ->exists();
    }

    /**
     * 用户商品经验值
     * @param $UID
     * @param $GID
     * @return bool
     */
    public function getUserGoodsIncome($UID, $GID)
    {
        $income = DB::table($this->table)
            ->where('user_id', $UID)
            ->where('goods_id', $GID)
            ->value('income');

        return (int)$income;
    }



    /*******************************日志*******************************/
    /**
     * 可升级商品经验变更日志
     * @param array $data
     * @return bool
     */
    function addUserGoodsIncomeLog(array $data)
    {
        return DB::table($this->tableLog)->insert($data);
    }
    /*******************************日志*******************************/

    /*******************************tableMallGoodsUpgradeConfig*******************************/
    function getUpgradeInfoByInc($income, $GID)
    {
        $sql = 'select * from ' . $this->tablePrefix . $this->tableMallGoodsConfig . ' where goods_id = '. $GID . ' and intimate<=' . $income . ' and (intimate_max>=' . $income . ' or intimate_max=0) limit 1';
        $bindings = array($income, $income, $income);
        $re = DB::select($sql, $bindings);
        return $this -> objToArray($re[0]);

    }
    /*******************************tableMallGoodsUpgradeConfig*******************************/



}
